package solutions;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2023-12-09
 * Time: 18:11
 */
public class Solution6 {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        //建立四个链表结点,表示两个链表a,b的头结点和尾结点:
        ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;
        while(pHead != null) {
            if(pHead.val < x) {
                if(as == null) {
                    as = pHead;
                    as.next = ae;
                }else {
                    ae = pHead;
                    ae = ae.next;
                }
            }else {
                if(bs == null) {
                    bs = pHead;
                    bs.next = be;
                }else {
                    be = pHead;
                    be = be.next;
                }

            }
            pHead = pHead.next;
        }
        if(as == null) {
            be.next = null;
            return be;
        }else {
            ae.next = bs;
            return ae;
        }

    }
}
